package ysq.xyg.mob.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.mob.dao.MobIconDao;
import ysq.xyg.mob.model.po.MobIcon;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.mob.model.vo.MobIconVo;
import ysq.xyg.mob.service.MobIconService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 移动端模块-导航按钮service实现
 *
 * @author shengQiang yu
 * @date 2019-10-24 17:09:26
 */
@Service("mobIconService")
public class MobIconServiceImpl extends BaseServiceImpl<MobIcon> implements MobIconService {

    @Resource
    private MobIconDao mobIconDao;

    private MobIcon mobIcon;

    @Autowired
    public void setBaseDao(MobIconDao mobIconDao) {
        this.baseDao = mobIconDao;
    }


    /**
     * 分页查询
     *
     * @param mobIconVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:26
     */
    public ReturnEntity page(MobIconVo mobIconVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("  sort desc,update_time desc");
        List<MobIcon> list = acceptList(mobIconVo);
        PageInfo<MobIcon> page=new PageInfo<MobIcon>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param mobIconVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:26
     */
    public ReturnEntity addOrUpdate(MobIconVo mobIconVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(mobIconVo.getId())){
            mobIcon=new MobIcon(currentUser);
            isUpate=false;
        }else{
            mobIcon=selectByPrimaryKey(mobIconVo.getId());
            if(ObjectUtil.isNull(mobIcon)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            mobIcon.setUpdateTime(new Date());
            mobIcon.setUpdateUser(currentUser.getId());
        }
        mobIcon.setImage(mobIconVo.getImage());
        mobIcon.setUrl(mobIconVo.getUrl());
        mobIcon.setName(mobIconVo.getName());
        mobIcon.setSort(mobIconVo.getSort());
        mobIcon.setShowType(mobIconVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(mobIcon);
        }else{
            num=insert(mobIcon);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:26
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        MobIcon mobIcon=selectByPrimaryKey(id);
        if(mobIcon!=null){
            mobIcon.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            mobIcon.setUpdateTime(new Date());
            mobIcon.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(mobIcon);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param mobIconVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-24 17:09:26
     */
    public List<MobIcon> acceptList(MobIconVo mobIconVo){
        Example example = new Example(MobIcon.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(mobIconVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",mobIconVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(mobIconVo.getShowType())){
            criteria.andEqualTo("showType",mobIconVo.getShowType());
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return mobIconDao.selectByExample(example);
    }
}
